package com.platform.data.handle.converter.impl;

import com.platform.data.common.CommonConstant;
import com.platform.data.handle.converter.DataTypeEnum;
import com.platform.data.handle.converter.AbstractDataTypeProcessHandler;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.RoundingMode;

/**
 * 浮点型
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/5/13 13:35
 */
public class BigDecimalTypeProcessHandler extends AbstractDataTypeProcessHandler {

    /**
     * 浮点型
     *
     * @param typeEnum typeEnum
     * @param fieldName fieldName
     * @param fieldValue fieldValue
     * @return {@link Object}
     */
    @Override
    public Object process(DataTypeEnum typeEnum, String fieldName, String fieldValue) {
        if(!NumberUtils.isCreatable(fieldValue)){
            log.warn("BigDecimalTypeProcessHandler.process|字符串转浮点数，输入值不是数字，转换失败返回原值，属性名称：{}，属性值：{}", fieldName, fieldValue);
            return fieldValue;
        }
        String placeNumber = typeEnum.getType().substring(CommonConstant.ONE, CommonConstant.TWO);
        return NumberUtils.toScaledBigDecimal(fieldValue, Integer.parseInt(placeNumber), RoundingMode.HALF_UP);
    }
}
